package com.wang.userscenter.common.util;

import java.util.Random;

/**
 * 正则表达工式相关的工具类
 */
public class RegexUtil {
	/**
	 * 随机生成字符串
	 * @param type: 生成的类型:0:纯数字,1:纯字母;2:字母+数字
	 * @param num:	表达生成的位数
	 * @return	按照要求的随机数;
	 */
	public String randStr(String type, int num) {
		StringBuilder sb = new StringBuilder();
		/* 原始字符串 */
		String sourceString = ConstatFinalUtil.ALLSTR_NUM;
		if ("1".equalsIgnoreCase(type)) {
			sourceString = ConstatFinalUtil.ALLSTR_CHAR;
		} else if ("2".equalsIgnoreCase(type)) {
			sourceString = ConstatFinalUtil.ALLSTR_CHAR_NUM;
		}
//		System.out.println("==sourceString==" + sourceString);
		
		Random random = new Random();
		for(int i = 0 ; i < num ; i ++) {
			int randInt = random.nextInt(sourceString.length());
			/* 返回指定字符串指定位置上的char字符 */
			char chTemp = sourceString.charAt(randInt);
//			System.out.println("=randInt==" + randInt + "====" + chTemp);
			sb.append(chTemp);
		}
		return sb.toString();
	}
	
	public static void main(String[] args) {
		RegexUtil regexUtil = new RegexUtil();
		for(int i = 0 ; i < 20 ; i ++) {
			String randStr = regexUtil.randStr("2", 6);
			System.out.println("==randStr==" + randStr);
		}
	}
}
